import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent, STColumnBadge, STData, STChange } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { OssFileUploadComponent } from './upload/upload.component';
import { environment } from '@env/environment';

@Component({
  selector: 'app-sys-file',
  templateUrl: './file.component.html',
})
export class OssFileComponent implements OnInit {
  isGeneralQuery = true;
  selectedRows: STData[] = [];
  keyword: string = '';
  
  sexBadge: STColumnBadge = {};

  constructor(private http: _HttpClient, private msgSrv: NzMessageService, private modal: ModalHelper) {}

  url = `${environment.oss.ctx}/ossFile/findByQueryParams`;

  searchSchema: SFSchema = {
    properties: {
      id: {
        type: 'string',
        title: '主键ID',
      },
      batchId: {
        type: 'string',
        title: '批次ID',
      },
      name: {
        type: 'string',
        title: '名称',
      },
    },
  };

  @ViewChild('st', { static: false }) st!: STComponent;
  columns: STColumn[] = [
    { title: 'ID', index: 'id', type: 'checkbox' },
    { title: 'ID', index: 'id' },
    { title: '批次ID', index: 'batchId' },
    { title: '名称', index: 'name' },
    { title: '扩展名', index: 'extName' },
    { title: '类型', index: 'contentType' },
    { title: '路径', index: 'path' },
    { title: '大小', index: 'size' },
    { title: '仓库', index: 'store' },
    { title: 'MD5', index: 'md5' },
    { title: '创建时间', index: 'createdDate', type: 'date' },
    { title: '创建人', index: 'createdBy' },
    {
      title: '操作',
      width: 100,
      fixed: 'right',
      buttons: [
        // { text: '查看', click: (item: any) => `/form/${item.id}` },
        {
          icon: 'download',
          type: 'none',
          click: (record: any) => {
            window.location.href = `${environment.SERVER_URL}${environment.oss.ctx}/ossFile/download/${record.id}`;
          },
          tooltip: `下载`,
        },
        {
          tooltip: `删除`,
          icon: 'delete',
          type: 'del',
          pop: {
            title: '是否确定删除?',
            okType: 'danger',
            icon: 'star',
          },
          click: (record, _modal, comp) => this.delete(record, _modal, comp),
        },
      ],
    },
  ];

  ngOnInit() {}

  delete(record: any, _modal: any, comp: any) {
    this.http.delete(`${environment.oss.ctx}/ossFile/${record.id}`).subscribe(res => {
      this.msgSrv.success(`成功删除`);
      this.st.reload();
    });
  }

  add() {
    this.modal.createStatic(OssFileUploadComponent, { i: {} }).subscribe(() => this.st.reload());
  }

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  
  deletes() {
    const ids = this.selectedRows.map((row)=>{return row.id})
    this.http.post(`${environment.oss.ctx}/ossFile/deleteByIds`,ids).subscribe(res => {
      this.msgSrv.success(`成功删除`);
      this.st.reload();
    });
  }
}
